import { defineStore } from 'pinia'
import { createI18n } from 'vue-i18n'
import Cookies from 'js-cookie'
import langInfo from '@/lang'
import appInfo from '@/stores/config/appInfo'

let langList = []
for (let key of Object.keys(langInfo.messages)) {
  langList.push({
    value: key,
    label: langInfo.messages[key].langName
  })
}
let lang = Cookies.get('lang')
if (!lang || !langInfo.langArrays.includes(lang)) {
  lang = langInfo.defaultLang
}
let cookieProperties = {
  path: appInfo.routeBase,
  expires: appInfo.cookieExpires
}
const i18n = createI18n({
  legacy: false,
  globalInjection: true,
  locale: lang,
  messages: langInfo.messages
})
Cookies.set('lang', lang, cookieProperties)
export const useAppStore = defineStore('app', {
  state: () => ({
    mainHeight: 0,
    appInfo: appInfo,
    cacheList: [],
    tagNavList: Cookies.get('tagNavList') ? JSON.parse(Cookies.get('tagNavList')) : [],
    isMini: false,
    i18n: i18n,
    sidebar: {
      opened: Cookies.get('sidebarStatus') !== '0'
    },
    lang: {
      langList: langList,
      langMessages: langInfo.messages,
      lang: lang
    },
    user: {
      userInfo: {},
      loginNo: Cookies.get('loginNo'),
      remember: Cookies.get('remember') === 'true',
      token: Cookies.get('token'),
      tokenType: Cookies.get('token_type'),
      scope: Cookies.get('scope'),
      menuList: []
    },
    routeState: Cookies.get('routeState') ? JSON.parse(Cookies.get('routeState')) : {}
  }),
  actions: {
    setMainHeight(payload) {
      this.mainHeight = payload
    },
    closeSlidebar() {
      Cookies.set('sidebarStatus', 0, cookieProperties)
      this.sidebar.opened = false
    },
    openSlidebar() {
      Cookies.set('sidebarStatus', 1, cookieProperties)
      this.sidebar.opened = true
    },
    setCacheList(payload) {
      this.cacheList = payload
    },
    setTagNavList(payload) {
      this.tagNavList = payload
      Cookies.set('tagNavList', JSON.stringify(payload), cookieProperties)
    },
    setMini(payload) {
      this.isMini = payload
    },
    /**
     * 设置语言
     */
    setLang(payload) {
      this.lang.lang = payload
      Cookies.set('lang', payload, cookieProperties)
      this.i18n.global.locale = payload
    },
    setLoginNo(payload) {
      this.user.loginNo = payload
      Cookies.set('loginNo', payload, cookieProperties)
    },
    setRemember(payload) {
      this.user.remember = payload
      Cookies.set('remember', payload, cookieProperties)
    },
    setToken(payload) {
      this.user.token = payload
      Cookies.set('token', payload, cookieProperties)
    },
    setTokenType(payload) {
      this.user.tokenType = payload
      Cookies.set('token_type', payload, cookieProperties)
    },
    setScope(payload) {
      this.user.scope = payload
      Cookies.set('scope', payload, cookieProperties)
    },
    setWarmToken(payload) {
      Cookies.set('Warm-TokenName', 'Authorization', cookieProperties)
      Cookies.set('Warm-Authorization', payload, cookieProperties)
    },
    loginOut() {
      this.user.token = ''
      this.user.tokenType = ''
      this.user.scope = ''
      this.user.menuList = []
      this.user.userInfo = {}
      Cookies.remove('token', cookieProperties)
      Cookies.remove('token_type', cookieProperties)
      Cookies.remove('scope', cookieProperties)
      Cookies.remove('Warm-TokenName', cookieProperties)
      Cookies.remove('Warm-Authorization', cookieProperties)
    },
    setMenuList(payload) {
      this.user.menuList = payload
    },
    setUserInfo(payload) {
      this.user.userInfo = payload
    },
    setRouteState(payload, expires = 1) {
      this.routeState = payload
      Cookies.set('routeState', JSON.stringify(payload), {
        path: cookieProperties.path,
        expires: expires
      })
    }
  }
})
